// pages/shops/check.js
const app = getApp();

let req = require('../../utils/request.js');
let api = require('../../utils/api.js');
let util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    answer: '',
    coupon: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中'
    })
    let id = options.id, _this = this, token = wx.getStorageSync('user_token');
    req.request(api.getOrderInfoById, {data: { id: id, user_token: token }}, function(res) {
      res = res.data;
      _this.setData({id: id, info: res, people: JSON.parse(res.people), wrapper: 'hide'});
      wx.hideLoading();
    })
  },
  onUnload: function () {
    let _this = this, token = wx.getStorageSync('user_token');
    req.request(api.deleteUnReservedOrder, {data: {id: _this.data.id, user_token: token}}, function(res) {
    })
  },
  answer(e) {
    let _this = this, value = e.detail.value;
    _this.setData({answer: value})
  },
  insertCoupon(e) {
    let _this = this, value = e.detail.value;
    _this.setData({ coupon: value })
  },
  booking() {
    let _this = this, id = _this.data.id, answer = _this.data.answer, extra_question = _this.data.info.extra_question, user_token = wx.getStorageSync('user_token'), reservation_id = _this.data.reservation_id, coupon = _this.data.coupon;
    if(!reservation_id) {
      if(extra_question != null && !answer) {
        wx.showToast({
          title: '请填写附加问题的回答',
          icon: 'none',
          duration: 2e3
        })
        return false;
      }

      wx.showLoading({
        title: '加载中'
      })

      req.request(api.createReservationOrder, { data: { id: id, answer: answer, user_token: user_token, coupon: coupon } }, function (res) {
        wx.hideLoading();
        res = res.data;
        if (res.code == 1) {
          reservation_id = res.data.reservation_id
          _this.setData({ reservation_id: reservation_id });
          _this.pay(reservation_id);
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      })
    }else {
      _this.pay(reservation_id);
    }
  },
  pay(reservation_id) {
    //模拟用户支付
    let _this = this, id = _this.data.id, token = wx.getStorageSync('user_token')

    wx.showLoading({
      title: '加载中'
    })
    
    req.request(api.getPaymentsData, { data: { id: id, reservation_id: reservation_id, user_token: token } }, function (res) {
      res = res.data;
      if(res.code != 1) {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        });
      }else {
        let data = res.data;
        data.success = function(res) {
          wx.showToast({
            title: '支付成功！正在为您生成电子凭证，您可以在“我的订单”中查看并使用凭证',
            icon: 'none',
            duration: 4000
          });

          setTimeout(function() {
            wx.redirectTo({
              url: '/pages/user/indent',
            })
          }, 4000)

          req.request(api.checkUserPayResult, { data: { id: id, reservation_id: reservation_id, user_token: token } }, function(res) {})
          
        }
        data.fail = function(res) {
          wx.showToast({
            title: '取消支付，请在15分钟内支付，否则订单将过期失效',
            icon: 'none'
          });
        }

        wx.requestPayment(data);
      }
    })
  }
})